package cn.wj.school.cloud.utils;


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncryptAlgorithm {

    private static String MD5 = "md5";

    private static String SHA = "sha";

    private static final int BUFFER_SIZE = 3;

    private static final int BUFFER_COMPARE = 4;

    private static final int BUFFER_FLOAT = 16;

    private static final String DECODE = "ISO-8859-1";

    private EncryptAlgorithm() {

    }

    /**
     * 
     * <strong>Title : hexMD5<br>
     * </strong> <strong>Description : </strong>得到MD5加密后的str<br>
     * <strong>Created in 19:47 2019/1/3<br>
     * </strong>
     * <p>
     * 
     * @param str
     * @return String
     * @throws @author lidapeng<br>
     * @version <strong>v1.0.0</strong><br>
     *          <br>
     *          <strong>修改历史:</strong><br>
     *          修改人 | 修改日期 | 修改描述<br>
     *          -------------------------------------------<br>
     *          <br>
     *          <br>
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static String hexMD5(String str) {
        return toHex(encryptMD5(str.getBytes()));
    }

    /**
     * 
     * <strong>Title : hexSHA<br>
     * </strong> <strong>Description : </strong>得到sha加密后的str<br>
     * <strong>Created in 19:47 2019/1/3<br>
     * </strong>
     * <p>
     * 
     * @param str
     * @return String
     * @throws @author lidapeng<br>
     * @version <strong>v1.0.0</strong><br>
     *          <br>
     *          <strong>修改历史:</strong><br>
     *          修改人 | 修改日期 | 修改描述<br>
     *          -------------------------------------------<br>
     *          <br>
     *          <br>
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static String hexSHA(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        return toHex(encryptSHA(str.getBytes(DECODE)));
    }


    /**
     * 
     * <strong>Title : encryptMD5<br>
     * </strong> <strong>Description : </strong>MD5 加密<br>
     * <strong>Created in 19:47 2019/1/3<br>
     * </strong>
     * <p>
     * 
     * @param data
     * @return
     * @throws NoSuchAlgorithmException byte[]
     * @throws @author lidapeng<br>
     * @version <strong>v1.0.0</strong><br>
     *          <br>
     *          <strong>修改历史:</strong><br>
     *          修改人 | 修改日期 | 修改描述<br>
     *          -------------------------------------------<br>
     *          <br>
     *          <br>
     */
    private static byte[] encryptMD5(byte[] data) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance(MD5);
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("No such algorithm [" + MD5 + "]", e);
        }
        md5.update(data);
        return md5.digest();
    }

    /**
     * 
     * <strong>Title : encryptSHA<br>
     * </strong> <strong>Description : </strong>SHA 加密<br>
     * <strong>Created in 19:47 2019/1/3<br>
     * </strong>
     * <p>
     * 
     * @param data
     * @return byte[]
     * @throws @author lidapeng<br>
     * @version <strong>v1.0.0</strong><br>
     *          <br>
     *          <strong>修改历史:</strong><br>
     *          修改人 | 修改日期 | 修改描述<br>
     *          -------------------------------------------<br>
     *          <br>
     *          <br>
     * @throws NoSuchAlgorithmException
     */
    private static byte[] encryptSHA(byte[] data) throws NoSuchAlgorithmException {
        MessageDigest sha = MessageDigest.getInstance(SHA);
        sha.update(data);
        return sha.digest();
    }


    private static String toHex(byte[] buffer) {
        StringBuilder sb = new StringBuilder(buffer.length * BUFFER_SIZE);
        for (int i = 0; i < buffer.length; i++) {
            sb.append(Character.forDigit((buffer[i] & 0xf0) >> BUFFER_COMPARE, BUFFER_FLOAT));
            sb.append(Character.forDigit(buffer[i] & 0x0f, BUFFER_FLOAT));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(hexMD5("a123456"));
    }
}
